from django.shortcuts import render,redirect,reverse
from django.views import View
from articles.models import *
# Create your views here.

# 首页
class IndexView(View):
    def get(self, request):
        return render(request, 'index.html')




def manage_article(request):
    articles = Articles.objects.all()
    print(articles)
    return render(request, 'article.html',locals())
def add_article(request):
    articles = Articles.objects.all()
    print(articles)
    return render(request,"add-article.html",locals())
def update_article(request,tid):
    if request.method == "GET":
        article = Articles.objects.get(pk=tid)
        categories = Category.objects.all()
        print("*"*100)
        print(article.cid.cid)
        return render(request, "update-article.html",locals())
    else:
        print("^" * 100)
        print(tid)
        article = Articles.objects.get(pk=tid)
        article.content = request.POST.get("content")
        article.title = request.POST.get("title")
        article.author = request.POST.get("author")
        article.save()
        print(request.POST.get("content"))
        return redirect(reverse("articles:update_article",args=(tid,)))